from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers
from .models import Recruit, Enterprise, City


# 企业简单序列化器
class EnterpriseSerializer(serializers.ModelSerializer):
    recruits = serializers.PrimaryKeyRelatedField(label='招聘职位',read_only=True,many=True)

    class Meta:
        model = Enterprise
        fields = ("id","name","labels","logo","recruits","summary")


# 招聘简单序列化器
class RecommendSerializer(serializers.ModelSerializer):

    enterprise = EnterpriseSerializer(label='企业')

    class Meta:
        model = Recruit
        fields = ('id','jobname','salary','condition','education','type','city','createtime','enterprise','labels')


# 热门城市详细序列化器
class CityHostListSerializer(serializers.ModelSerializer):
    class Meta:
        model = City
        fields = "__all__"



# 公司详情序列化器
class EnterpriseDeatilsSerializer(serializers.ModelSerializer):
    recruits = RecommendSerializer(label='招聘信息', many=True)
    class Meta:
        model = Enterprise
        fields = ('id','recruits','name','summary','content','city','address','labels','coordinate','logo','url','visits','users')


# 招聘详情序列化器
class RecruitsDeatilsSerializer(serializers.ModelSerializer):

    enterprise = EnterpriseDeatilsSerializer(label='企业')

    class Meta:
        model = Recruit
        fields = "__all__"


# haystack序列化器
# class RecruitIndexSerializer(HaystackSerializer):
#     object = RecommendSerializer(read_only=True)
#
#     class Meta:
#         index_classes = [RecruitIndex]
#         # text 由索引类进行返回, object 由序列化类进行返回,第一个参数必须是text
#         fields = (
#             'text',  # 用于接收查询关键字
#             'object',  # 用于返回查询结果
            # 'id',
            # 'jobname',
            # 'salary',
            # 'condition',
            # 'education',
            # 'type',
            # 'city',
            # 'createtime',
            # 'enterprise',
            # 'labels'
        # )